<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ include file="/WEB-INF/page/common/commonPage.jsp"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>网站基本信息</title>
    <link rel="stylesheet" type="text/css" href="${resourcesUrl}/common/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="${resourcesUrl}/common/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="${resourcesUrl}/common/css/smartadmin-production.min.css">
    <style>
        .smart-form .input i.icon-append {
            right: 5px;
            padding-left: 3px;
            border-left: 1px solid #BDBDBD;
        }
        .smart-form .input input {
            padding-right: 30px;
        }
    </style>
</head>
<body>
    <%@ include file="/WEB-INF/page/common/header.jsp"%>
    <%@ include file="/WEB-INF/page/common/leftSideBar.jsp"%>

    <div id="main" role="main">
        <div id="ribbon">
            <ol class="breadcrumb">
                <li>首页</li>
                <li>网站基本信息</li>
            </ol>
        </div>
        <div id="content">
            <div class="row">
                <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                    <div class="jarviswidget">
                        <header>
                            <h2><i class="fa fa-pencil"></i> 网站基本信息</h2>
                        </header>
                        <div class="widget-body">
                            <form id="editForm" class="smart-form">
                                <fieldset>
                                    <c:if test="${empty params}">
                                        <div class="alert alert-warning">
                                            <strong>提示：</strong> 暂无参数数据
                                        </div>
                                    </c:if>
                                    <!-- 调试输出 -->
                                    <div style="display:none">
                                        <p>Params size: ${fn:length(params)}</p>
                                        <c:forEach items="${params}" var="paramItem">
                                            <p>Param: ${paramItem}</p>
                                            <p>ID: [${paramItem.paramId}]</p>
                                            <p>Name: [${paramItem.paramName}]</p>
                                            <p>Value: [${paramItem.paramValue}]</p>
                                        </c:forEach>
                                    </div>

                                    <c:forEach items="${params}" var="paramItem">
                                        <section>
                                            <label class="label">${paramItem.paramName}</label>
                                            <label class="input">
                                                <i class="icon-append fa fa-pencil"></i>
                                                <input type="hidden" name="paramIds" value="${paramItem.paramId}">
                                                <input type="text" name="paramValues" value="${paramItem.paramValue}" class="form-control">
                                            </label>
                                        </section>
                                    </c:forEach>
                                </fieldset>
                                <footer>
                                    <button type="submit" class="btn btn-primary">
                                        <i class="fa fa-save"></i> 保存
                                    </button>
                                    <button type="button" class="btn btn-default" onclick="window.location.reload()">
                                        <i class="fa fa-refresh"></i> 重置
                                    </button>
                                </footer>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="${resourcesUrl}/common/js/jquery-1.8.2.min.js"></script>
    <script src="${resourcesUrl}/common/js/bootstrap/bootstrap.min.js"></script>
    <script src="${resourcesUrl}/common/js/plugin/jquery-validate/jquery.validate.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#editForm").validate({
                rules: {
                    paramValues: {
                        required: true,
                        maxlength: 50
                    }
                },
                messages: {
                    paramValues: {
                        required: "请输入参数值",
                        maxlength: "参数值不能超过50个字符"
                    }
                },
                submitHandler: function(form) {
                    var paramIds = [];
                    var paramValues = [];
                    $("input[name='paramIds']").each(function() {
                        paramIds.push($(this).val());
                    });
                    $("input[name='paramValues']").each(function() {
                        paramValues.push($(this).val());
                    });
                    
                    $.ajax({
                        url: '${base}/param/updateParam',
                        type: 'POST',
                        data: {
                            paramIds: paramIds.join(','),
                            paramValues: paramValues.join(',')
                        },
                        success: function(data) {
                            if(data.RET_CODE.toLowerCase() === 'success') {
                                alert('保存成功');
                                window.location.reload();
                            } else {
                                alert(data.RET_OBJ || '保存失败');
                            }
                        }
                    });
                    return false;
                }
            });
        });
    </script>
    <%@ include file="/WEB-INF/page/common/footer.jsp"%>
</body>
</html> 